-include env.mk

UPLOAD_DIR=clean_src
SRC_DIR=om-manager
MSG_PREFIX=*********************

VERSION=`$(PYTHON) version.py --get_version_string`
DISTR_FNAME=om-manager_$(VERSION)

all: bdist

bdist: mkUploadableSrc build_dists rmUplodableSrc

mkUploadableSrc: mkUploadableFiles cleanUploadDir

mkUploadableFiles:
	@echo "$(MSG_PREFIX)Making upload dir$(MSG_PREFIX)"
	rm -rf "$(UPLOAD_DIR)"
	mkdir "$(UPLOAD_DIR)"
	cp -r "$(SRC_DIR)" "$(UPLOAD_DIR)"
	# merging with 'documentation' folder
	cp -r ./documentation "$(UPLOAD_DIR)/$(SRC_DIR)"
	$(PYTHON) version.py --get_full_data > \
	    "$(UPLOAD_DIR)/$(SRC_DIR)/documentation/version.txt"

rmUplodableSrc:
	rm -r "$(UPLOAD_DIR)/$(SRC_DIR)"

cleanUploadDir:
	@echo "$(MSG_PREFIX)Performing cleanup$(MSG_PREFIX)"
	-$(FIND) "$(UPLOAD_DIR)" -name .svn | xargs rm -r
	-$(FIND) "$(UPLOAD_DIR)" -name \*.pyc -delete
	-$(FIND) "$(UPLOAD_DIR)" -name \*.pyo -delete
	-$(FIND) "$(UPLOAD_DIR)" -name \*.swp -delete
	# remove all config files too (plugins must have defaults)
	-$(FIND) "$(UPLOAD_DIR)" -name \*.cfg -delete
	# no need in XRCed configuration files in non-repository
	# versions
	-$(FIND) "$(UPLOAD_DIR)" -name \*.xcfg -delete
	# no need in logs ether
	-rm "$(UPLOAD_DIR)/$(SRC_DIR)"/app/log/*

build_dists:
	@echo "$(MSG_PREFIX)Assembling distributions$(MSG_PREFIX)"
	$(MAKE) mkArch
	# Make *.app only if run under OSX
	if [ "`uname -s`" == "Darwin" ] ; then $(MAKE) mkApp; fi
	# Create *.exe on Windows
	if [[ "`uname -s`" =~ "CYGWIN" ]] ; then $(MAKE) mkExe; fi

mkArch:
	@echo "$(MSG_PREFIX)Creating *.tar.gz$(MSG_PREFIX)"
	tar cfz "$(UPLOAD_DIR)"/$(DISTR_FNAME).tar.gz "$(UPLOAD_DIR)/$(SRC_DIR)"

	@echo "$(MSG_PREFIX)Creating *.zip$(MSG_PREFIX)"
	zip -r "$(UPLOAD_DIR)/$(DISTR_FNAME).zip" "$(UPLOAD_DIR)/$(SRC_DIR)"

mkApp:
	@echo "$(MSG_PREFIX)Creating *.app$(MSG_PREFIX)"
	-rm -r build
	python setup.py py2app
	# not wery nice to do here, but saves 10mb of space
	rm -r "$(UPLOAD_DIR)/om-manager.app/Contents/Resources/lib/python2.5/wx/tools"
	# Creating folder that will be included into *.dmg
	mkdir "$(UPLOAD_DIR)/om-manager.dmg.src"
	mv "$(UPLOAD_DIR)/om-manager.app" "$(UPLOAD_DIR)/om-manager.dmg.src"
	# Include all documentation files into root of disk image
	cp "$(UPLOAD_DIR)/$(SRC_DIR)/documentation"/* "$(UPLOAD_DIR)/om-manager.dmg.src"
	hdiutil create \
	    -fs HFS+ -format UDZO -imagekey zlib-level=9 \
	    -volname "om-manager" \
	    -srcfolder "$(UPLOAD_DIR)/om-manager.dmg.src" \
	    -o "$(UPLOAD_DIR)/$(DISTR_FNAME).dmg"
	# It is nice to cleanup, isn't it?
	rm -r "$(UPLOAD_DIR)/om-manager.dmg.src"

mkExe:
	@echo "$(MSG_PREFIX)Creating *.exe$(MSG_PREFIX)"
	-rm -r build
	python setup.py py2exe
	-rm -r build
	mv dist "dist.win32"
	zip -r "$(UPLOAD_DIR)/$(DISTR_FNAME).win32.zip" "dist.win32"
	rm -r "dist.win32"

upload:
	./upload_all.sh
